package com.rabbitmqproducer.config;

import jakarta.annotation.Resource;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ConfirmConfig {
	@Bean
	RabbitTemplate createRabbitTemplate(ConnectionFactory connectionFactory) {
		RabbitTemplate rabbitTemplate=new RabbitTemplate();
		rabbitTemplate.setConnectionFactory(connectionFactory);
		rabbitTemplate.setMandatory(true);//开启Mandatory
		rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
			@Override
			public void confirm(CorrelationData correlationData, boolean b, String s) {
				System.out.println("ConfirmCallback相关数据: " + correlationData + ", ack确认情况: " + b + ", cause原因: " + s);
			}
		});
		rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
			@Override
			public void returnedMessage(ReturnedMessage returnedMessage) {
				System.out.println("ReturnCallback消息："+returnedMessage.getMessage());
				System.out.println("ReturnCallback回应码："+returnedMessage.getReplyCode());
				System.out.println("ReturnCallback回应信息："+returnedMessage.getReplyText());
				System.out.println("ReturnCallback交换机："+returnedMessage.getExchange());
				System.out.println("ReturnCallback路由键："+returnedMessage.getRoutingKey());
			}
		});
		return rabbitTemplate;
	}
}
